package com.dianrui.operation.util;

/**
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * Created by leilei on 2017/5/5.
 */

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import com.blankj.utilcode.util.LogUtils;

import java.util.Iterator;
import java.util.Stack;

/**
 * Desction:应用程序Activity管理类：用于Activity管理和应用程序退出
 * Author:pengjianbo
 * Date:15/9/17 下午4:48
 */
@SuppressWarnings("ALL")
public class ActivityManagerUtils {
    /**
     * stack<Activity>存放activity
     *
     */
    private static Stack<Activity> mActivityStack;

    /**
     * 管理activity
     *
     */
    private static ActivityManagerUtils mActivitiesManager;

    /**
     * construct
     *
     */
    private ActivityManagerUtils() {
    }

    /**
     * 单例
     *
     * @return
     */
    public static ActivityManagerUtils getInstance() {
        if (null == mActivitiesManager) {
            mActivitiesManager = new ActivityManagerUtils();
            if (null == mActivityStack) {
                mActivityStack = new Stack<Activity>();
            }
        }
        return mActivitiesManager;
    }

    /**
     * 栈大小
     *
     * @return
     */
    public int stackSize() {
        return mActivityStack.size();
    }

    /**
     * 获取当前Activity（栈顶activity）
     *
     * @return
     */
    public Activity getCurrentActivity() {
        Activity activity = null;
        try {
            activity = mActivityStack.lastElement();
        } catch (Exception e) {
            return null;
        }
        return activity;
    }

    /**
     * 移除栈顶activity
     *
     */
    public void moveActivity() {
        Activity activity = mActivityStack.lastElement();
        if (null != activity) {
            activity.finish();
            mActivityStack.remove(activity);
            activity = null;
        }
    }

    public void closeTopActivity(Activity activity){
        if(null != activity){
            Activity topActivity=mActivityStack.peek();
            while (!activity.getClass().getSimpleName().equals(topActivity.getClass().getSimpleName())){
                mActivityStack.remove(topActivity);
                topActivity.finish();
                topActivity=mActivityStack.peek();
            }
        }
    }

    public void closeActivity(Class<?> clazz){
        if(null != clazz){
            for(int index=mActivityStack.size()-1;index>0;index--){
                Activity element=mActivityStack.elementAt(index);
                if(element!=null&&clazz.getSimpleName().equals(element.getClass().getSimpleName())){
                    mActivityStack.remove(element);
                    element.finish();
                }
            }
        }
    }

    public boolean existActivityInTask(Class<?> clazz){
        if(null != clazz&&mActivityStack.size()>0){
            for(int index=mActivityStack.size()-1;index>0;index--){
                Activity element=mActivityStack.elementAt(index);
                if(clazz.getSimpleName().equals(element.getClass().getSimpleName()))
                    return true;
            }
        }
        return false;
    }

    public void popTopActivityInTask(){
        if(null != mActivityStack&&mActivityStack.size()>0){
            Activity topActivity=mActivityStack.peek();
            if(topActivity!=null){
                mActivityStack.remove(topActivity);
                topActivity.finish();
            }
        }
    }

    /**
     * 结束指定的Activity
     *
     * @param activity
     */
    public void moveActivity(Activity activity) {
        if (null != activity) {
            activity.finish();
            mActivityStack.remove(activity);
            activity = null;
        }
    }

    /**
     * 添加Activity到堆栈
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        mActivityStack.add(activity);
    }

    public void removeActivity(Activity activity){
        if (null != activity) {
            mActivityStack.remove(activity);
        }
    }

    /**
     * 结束所有Activity
     *
     */
    public void moveAllActivities() {
        while (!mActivityStack.isEmpty()) {
            Activity activity = getCurrentActivity();
            if (null == activity) {
                break;
            }
            activity.finish();
            moveActivity(activity);
        }
    }


    public void moveAllActivitiesExcept(Class<?> keepActivity) {
        try {
            Iterator<Activity> iterator = mActivityStack.iterator();
            Activity activity = null;
            while (iterator.hasNext()) {
                activity = iterator.next();
                if (!activity.getClass().equals(keepActivity)) {
                    activity.finish();
                    iterator.remove();
                    activity = null;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * 结束指定类名的Activity
     *
     * @param cls
     */
    public void moveSpecialActivity(Class<?> cls) {
        try {
            Iterator<Activity> iterator = mActivityStack.iterator();
            Activity activity = null;
            while (iterator.hasNext()) {
                activity = iterator.next();
                if (activity.getClass().equals(cls)) {
                    activity.finish();
                    iterator.remove();
                    activity = null;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 退出应用程序
     *
     */
    @SuppressWarnings("deprecation")
    public void appExit(Context context) {
        try {
            moveAllActivities();
            ActivityManager activityMgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.restartPackage(context.getPackageName());
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (mActivityStack == null) {
            return;
        }
        if (activity != null) {
            mActivityStack.remove(activity);
            LogUtils.e("sssss", activity.getLocalClassName().toString() + "--被销毁");
            activity.finish();
            activity = null;
        }
    }
}